﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace FrostGraphics.Umbraco.ContactForm {
  public class ContactFormFactory {

    private DataAccess da = new DataAccess();
    private ContactFormElement formElement;

    public ContactFormFactory(String formName) {
      formElement = da.ReadFormFromConfig(formName);

      if (formElement == null) {
        throw new NullReferenceException("The section could not be found in the configurationfile!");
      }
    }

    public void AddControlsTo(Control control) {

      // Insert start tag
      AddFormStartTag(control, formElement.Name);

      // Insert form fields
      ParseFormFields(formElement.FormFields, control);

      // Insert end tag
      AddFormEndTag(control);

    }

    private void ParseFormFields(ContactFormFieldCollection formFields, Control control) {
      foreach (ContactFormFieldElement formField in formFields) {
        AddFormItemStartTag(control);

        Label label = new Label();
        label.Text = formField.Label;
        control.Controls.Add(label);

        AddFormItemSeparatorTag(control);

        Control formControl = CreateControlFromField(formField);
        control.Controls.Add(formControl);

        AddFormItemEndTag(control);
      }
    }

    private Control CreateControlFromField(ContactFormFieldElement formField) {
      String type = formField.Type;
      String name = formField.Name;
      String defaultValue = formField.DefaultValue;
      Control control;

      switch (type) {

        case "TextBox":
          TextBox textBox = new TextBox();
          textBox.Text = defaultValue;
          control = textBox;
          break;

        case "TextArea":
          TextBox textArea = new TextBox();
          textArea.Text = defaultValue;
          textArea.TextMode = TextBoxMode.MultiLine;
          control = textArea;
          break;

        case "FileUpload":
          FileUpload fileUpload = new FileUpload();
          control = fileUpload;
          break;

        case "CheckBox":
          CheckBox checkBox = new CheckBox();
          checkBox.Checked = bool.Parse(defaultValue);
          control = checkBox;
          break;

        case "Dropdown":
          control = CreateDropdownFromOptions(formField.DropdownOptions);
          break;

        default:
          return null;

      }

      control.ID = name;
      return control;
    }

    private DropDownList CreateDropdownFromOptions(DropdownCollection options) {
      DropDownList dropdown = new DropDownList();

      if (options.Count > 1) {
        foreach (DropdownElement option in options) {
          ListItem listItem = new ListItem();
          listItem.Text = option.Label;
          listItem.Value = option.Name;
          dropdown.Items.Add(listItem);
        }
      }
      else {
        return null;
      }

      return dropdown;
    }

    #region Literals
    private void AddFormStartTag(Control control, String id) {
      AddLiteral("<dl id=\"" + id + "\" class=\"contactForm\">\n", control);
    }

    private void AddFormEndTag(Control control) {
      AddLiteral("\n</dl>", control);
    }

    private void AddFormItemStartTag(Control control) {
      AddLiteral("\n  <dt>", control);
    }

    private void AddFormItemSeparatorTag(Control control) {
      AddLiteral("</dt>\n  <dd>", control);
    }

    private void AddFormItemEndTag(Control control) {
      AddLiteral("</dd>\n", control);
    }

    private void AddLiteral(String text, Control control) {
      Literal literal = new Literal();
      literal.Text = text;
      control.Controls.Add(literal);
    }
    #endregion

  }
}